//
//  SinaController.m
//  kankanVideo
//
//  Created by wangbo on 7/10/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "SinaController.h"

static SinaController *sinaController;
static WBEngine *weiboEngine;
static NSString *ak;
static NSString *as;
//static WBSendView *weiboSendView;
//static NSString *ak=[[NSString alloc]initWithString:@"4281330846"];


@implementation SinaController
@synthesize delegate;

+(SinaController*)shareInstance{
    if (!sinaController) {
        sinaController=[[SinaController alloc]init];
        ak=[[NSString alloc]initWithString:@"4281330846"];
        as=[[NSString alloc]initWithString:@"aadc4acbe227004e391803af30fa6224"];
        
    }
    return sinaController;
}
-(BOOL)isLogin{
    if(!weiboEngine){
        weiboEngine=[[WBEngine alloc]initWithAppKey:ak appSecret:as];
        [weiboEngine setDelegate:self];
        [weiboEngine setRedirectURI:@"http://"];  
        [weiboEngine setIsUserExclusive:NO];
    }
    NSArray* _arr=[[MyCoreData shareInstance]queryConfig];
    if (_arr.count==0) {
        return NO;
    }else{
        ConfigData *_data=(ConfigData*)[_arr objectAtIndex:0];
        if ([_data.token isEqualToString:@""]) {
            return NO;
        }else{
            [weiboEngine setUserID:_data.userid];
            [weiboEngine setAccessToken:_data.token];
            return YES;
            /*if(!weiboEngine){
                weiboEngine=[[WBEngine alloc]initWithAppKey:ak appSecret:as];
                [weiboEngine setDelegate:self];
                [weiboEngine setRedirectURI:@"http://"];  
                [weiboEngine setIsUserExclusive:NO];
            }
            [weiboEngine setUserID:_data.userid];
            [weiboEngine setAccessToken:_data.token];
            return [weiboEngine isLoggedIn];*/
            //return YES;
        }
    }
}

-(NSDictionary*)getSinaInfo{
    ConfigData *_data=(ConfigData*)[[[MyCoreData shareInstance]queryConfig] objectAtIndex:0];
    return [[NSDictionary alloc]initWithObjects:[NSArray arrayWithObjects:_data.token,_data.userid,_data.name, nil] forKeys:[NSArray arrayWithObjects:@"token",@"userid",@"name", nil]];
    //[dict setValue:_data.token forKey:@"token"];
    //[dict setValue:_data.userid forKey:@"userid"];
    //[dict setValue:_data.name forKey:@"name"];
}

-(void)login:(UIViewController*)_root{
    [weiboEngine setRootViewController:_root];
    [weiboEngine logIn];
}
-(void)sendWithContent:(NSString *)_content withImage:(UIImage *)_image{
    NSLog(@"sendwithcontent");
    [weiboEngine sendWeiBoWithText:_content image:_image];
}
-(void)engineDidLogIn:(WBEngine *)engine{
    loginflag=YES;
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:2];
    [params setObject:engine.accessToken forKey:@"access_token"];  
    [params setObject:engine.userID forKey:@"uid"];  
    [weiboEngine loadRequestWithMethodName:@"users/show.json"  
                               httpMethod:@"GET"  
                                   params:params  
                             postDataType:kWBRequestPostDataTypeNone  
                         httpHeaderFields:nil];
}
- (void)engine:(WBEngine *)engine didFailToLogInWithError:(NSError *)error{
    NSLog(@"didFailToLogInWithError:%@",error);
}
// When you use the WBEngine's request methods,
// you may receive the following four callbacks.
- (void)engineNotAuthorized:(WBEngine *)engine{
    NSLog(@"engineNotAuthorized");
}
- (void)engineAuthorizeExpired:(WBEngine *)engine{
    NSLog(@"engineAuthorizeExpired");
}

- (void)engine:(WBEngine *)engine requestDidFailWithError:(NSError *)error{
    NSLog(@"requestDidFailWithError:%@",error);
}

- (void)engine:(WBEngine *)engine requestDidSucceedWithResult:(id)result  
{  
    //登录成功  
    NSLog(@"requestDidSucceedWithResult: %@", result);  
    if ([result isKindOfClass:[NSDictionary class]]&&loginflag)  
    {  
        NSDictionary *dict = (NSDictionary *)result;  
        NSLog(@"%@",dict);
        NSLog(@"username:%@",[dict objectForKey:@"name"]);
        [[MyCoreData shareInstance]updateConfig:engine.accessToken withUserid:engine.userID withName:[dict objectForKey:@"name"]];
        [delegate didLogin];
        //[dict objectForKey:@"name"];
    }
    loginflag=NO;
}
- (void)sendViewWillAppear:(WBSendView *)view{
    NSLog(@"sendViewWillAppear");
}
- (void)sendViewDidAppear:(WBSendView *)view{
    NSLog(@"sendViewDidAppear");
}
- (void)sendViewWillDisappear:(WBSendView *)view{
    NSLog(@"sendViewWillDisappear");
}
- (void)sendViewDidDisappear:(WBSendView *)view{
    NSLog(@"sendViewDidDisappear");
}

- (void)sendViewDidFinishSending:(WBSendView *)view{
    NSLog(@"sendViewDidFinishSending");
}
- (void)sendView:(WBSendView *)view didFailWithError:(NSError *)error{
    NSLog(@"sendView");
}

- (void)sendViewNotAuthorized:(WBSendView *)view{
    NSLog(@"sendViewNotAuthorized");
}
- (void)sendViewAuthorizeExpired:(WBSendView *)view{
    NSLog(@"sendViewAuthorizeExpired");
}

/*+(WBEngine*)weiboEngine{
    
}
+(WBSendView*)weiboSendView{
    
}
*/
@end
